package com.fhz.core_lib.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.fhz.core_lib.base.AppManager;
import com.fhz.core_lib.base.BaseApplication;

/**
 * 时间:2021/11/21
 *
 * @author msfeng
 * 简述: sp工具类
 */
public class SPUtils {
    public  static final String NICKNAME = "nickname";
    public  static final String TOKEN = "token";
    private static SPUtils instance = new SPUtils();
    private static SharedPreferences mSp;
    //单例
    private SPUtils(){
    }
    //得到单例
    public synchronized static SPUtils getInstance(){
        if(mSp == null){
            mSp = BaseApplication.getInstance().getSharedPreferences("login", Context.MODE_PRIVATE);
        }
        return instance;
    }

    //保存
    public void put(String key,Object value){
        if(value instanceof String){
            mSp.edit().putString(key, (String) value).commit();
        }else if(value instanceof Boolean){
            mSp.edit().putBoolean(key, (Boolean) value).commit();
        }else if(value instanceof  Integer){
            mSp.edit().putInt(key, (Integer) value).commit();
        }
    }
    //获取String类型数据
    public String getString(String key,String defValue){
        return mSp.getString(key,defValue);
    }
    //获取Boolean类型数据
    public Boolean getBoolean(String key,boolean defValue){
        return mSp.getBoolean(key,defValue);
    }//获取Int类型数据
    public int getInt(String key,int defValue){
        return mSp.getInt(key,defValue);
    }
}
